home *** CD-ROM | disk | FTP | other *** search
/ 17 Bit Software 6: Level 6 / 17 Bit - Level 6 (1998)(Epic Marketing)[!].iso / quartz / q0715.dms / q0715.adf / Devices / PrinterDevice / Example1.c < prev    next >
C/C++ Source or Header  |  1992-07-29  |  10KB  |  316 lines

  1. /***********************************************************/
  2. /*                                                         */
  3. /* Amiga C Encyclopedia (ACE) V3.0      Amiga C Club (ACC) */
  4. /* -------------------------------      ------------------ */
  5. /*                                                         */
  6. /* Book:    ACM Devices                 Amiga C Club       */
  7. /* Chapter: Printer Device              Tulevagen 22       */
  8. /* File:    Example1.c                  181 41  LIDINGO    */
  9. /* Author:  Anders Bjerin               SWEDEN             */
  10. /* Date:    92-04-27                                       */
  11. /* Version: 1.00                                           */
  12. /*                                                         */
  13. /*   Copyright 1992, Anders Bjerin - Amiga C Club (ACC)    */
  14. /*                                                         */
  15. /* Registered members may use this program freely in their */
  16. /*     own commercial/noncommercial programs/articles.     */
  17. /*                                                         */
  18. /***********************************************************/
  19.  
  20.  
  21.  
  22. /* This program demonstrates how you can use the Printer */
  23. /* Device to send (raw as well as translated) text to a  */
  24. /* printer.                                              */
  25.  
  26.  
  27.  
  28. #include <exec/types.h>       /* Data types.             */
  29. #include <exec/errors.h>      /* Exec error messages.    */
  30. #include <devices/printer.h>  /* Printer Device.         */
  31. #include <exec/io.h>          /* Standard request block. */
  32.  
  33.  
  34.  
  35. /* Declare how the printer request block look like: */
  36. union printerIO
  37. {
  38.   struct IOStdReq ios;
  39.   struct IODRPReq iodrp;
  40.   struct IOPrtCmdReq iopc;
  41. };
  42.  
  43. /* Declare a pointer to our reply port: */
  44. struct MsgPort *replymp = NULL;
  45.  
  46. /* Declare a pointer our printer request block: */
  47. union printerIO *printer_req = NULL;
  48.  
  49. /* Store the printer device error here: */
  50. UWORD printer_dever = TRUE;
  51.  
  52. /* Declare our data buffer: (25 characters) */
  53. BYTE buffer[] = "Anders Bjerin was here...";
  54.  
  55.  
  56.  
  57.  
  58. /* Declare our functions: */
  59.  
  60. /* Our main function: */
  61. void main();
  62.  
  63. /* Clears and removes everything nice and neatly: */
  64. void clean_up( BYTE error, STRPTR text );
  65.  
  66. /* Prints some information about the error: */
  67. void PrtError( BYTE error );
  68.  
  69. /* Sends characters (which are translated) to the printer: */
  70. BYTE PrintText(
  71.   union printerIO *ioreq,
  72.   BYTE *data,
  73.   ULONG length
  74. );
  75.  
  76. /* Sends raw (untranslated) characters to the printer: */
  77. BYTE PrintRaw(
  78.   union printerIO *ioreq,
  79.   BYTE *data,
  80.   ULONG length
  81. );
  82.  
  83.  
  84.  
  85. void main()
  86. {
  87.   /* Error number: */
  88.   BYTE error;
  89.   
  90.   
  91.   
  92.   /* Get a reply port: (No name, priority 0) */
  93.   replymp = (struct MsgPort *)
  94.     CreatePort( NULL, 0 );
  95.   if( !replymp )
  96.     clean_up( 0, "Could not create the reply port!" );
  97.  
  98.  
  99.  
  100.   /* Create the printer request block: */
  101.   printer_req = (union printerIO *)
  102.     CreateExtIO( replymp, sizeof(union printerIO) );
  103.   if( !printer_req )
  104.     clean_up( 0, "Not enough memory for the printer request block!" );
  105.  
  106.  
  107.  
  108.   /* Open the Printer Device: */
  109.   printer_dever = OpenDevice( "printer.device", 0, printer_req, 0 );
  110.   if( printer_dever )
  111.     clean_up( 0, "Could not open the Printer Device!" );
  112.  
  113.  
  114.  
  115.   /* Send some text to the printer: (Will be translated) */
  116.   error = PrintText( printer_req, buffer, 25 ); 
  117.   if( error )
  118.     PrtError( error );
  119.   else
  120.     printf( "Printing normal text...\n" );
  121.  
  122.  
  123.  
  124.   /* Send some raw (untranslated) text to the printer: */
  125.   error = PrintRaw( printer_req, buffer, 25 ); 
  126.   if( error )
  127.     PrtError( error );
  128.   else
  129.     printf( "Printing raw (untranslated) text...\n" );
  130.  
  131.  
  132.  
  133.   /* Clean up and quit: */
  134.   clean_up( 0, "The End!" );
  135. }
  136.  
  137.  
  138.  
  139. /* Close and return everything that has been */
  140. /* opened and allocated before we quit:      */
  141.  
  142. void clean_up( BYTE error, STRPTR text )
  143. {
  144.   /* Print some information about the problem: */
  145.   if( error )
  146.     PrtError( error );
  147.  
  148.   /* Close the Printer Device: */ 
  149.   if( !printer_dever )
  150.     CloseDevice( printer_req );
  151.  
  152.   /* Deallocate the printer request block: */
  153.   if( printer_req )
  154.     DeleteExtIO( printer_req, sizeof(union printerIO) );
  155.  
  156.   /* Remove the replyport: */
  157.   if( replymp )
  158.     DeletePort( replymp);
  159.  
  160.   /* Print the message: */
  161.   printf( "\n%s\n", text );
  162.  
  163.   /* Quit: */
  164.   exit( 0 );
  165. }
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172. /* PrtError() tells the user what went wrong. You give it the error code */
  173. /* you received, and PrtError() will print a short description of the    */
  174. /* problem. Useful when debugging. (Printer errors)                      */
  175. /*                                                                       */
  176. /* Synopsis: PrtError( error );                                          */
  177. /*                                                                       */
  178. /* error:    (BYTE) The error value you want to have explained.          */
  179.  
  180. void PrtError( BYTE error )
  181. {
  182.   switch( error )
  183.   {
  184.     /* EXEC error messages: (defined in "exec/errors.h") */
  185.     case IOERR_OPENFAIL:
  186.       printf( "Could not open the device!\n" );
  187.       break;
  188.  
  189.     case IOERR_ABORTED:
  190.       printf( "The request was aborted!\n" );
  191.       break;
  192.  
  193.     case IOERR_NOCMD:
  194.       printf( "Unknown Command!\n" );
  195.       break;
  196.  
  197.     case IOERR_BADLENGTH:
  198.       printf( "Bad length of the command - data!\n" );
  199.  
  200.  
  201.     /* Printer Device errors: (defined in "devices/printer.h") */
  202.     case PDERR_CANCEL:
  203.       printf( "User cancelled the request!\n" );
  204.       break;
  205.       
  206.     case PDERR_NOTGRAPHICS:
  207.       printf( "The printer does not support graphics!\n" );
  208.       break;
  209.  
  210.     case PDERR_BADDIMENSION:
  211.       printf( "The printer dimension is not valid!\n" );
  212.       break;
  213.       
  214.  
  215.     case PDERR_INTERNALMEMORY:
  216.       printf( "Not enough memory for the internal printer functions!\n" );
  217.       break;
  218.  
  219.     case PDERR_BUFFERMEMORY:
  220.       printf( "Not enough memory for the print buffer!\n" );
  221.       break;
  222.  
  223.     default:
  224.       printf( "An unknown error was reported! Error nr: %d\n", error );
  225.   }
  226. }
  227.  
  228.  
  229.  
  230. /* PrintText() sends characters (which will be translated) to the */
  231. /* printer. Since the printer device will use the Preference's    */
  232. /* settings, it will know to which port (parallel or serial) the  */
  233. /* printer is connected to, what type of printer it is, and what  */
  234. /* special settings (margins, density, quality mode etc) the user */
  235. /* have defined.                                                  */
  236. /*                                                                */
  237. /* Note! All characters which are sent with this function may be  */
  238. /* translated by Preferences before it is passed on to the        */
  239. /* printer.                                                       */
  240. /*                                                                */
  241. /* Synopsis: error = PrintText( io, data, length );               */
  242. /*                                                                */
  243. /* error:    (BYTE) PrintWrite() returns 0 if everything was OK,  */
  244. /*           else an error number is returned.                    */
  245. /*                                                                */
  246. /* io:       (union printerIO *) Pointer to a printer request     */
  247. /*           block.                                               */
  248. /*                                                                */
  249. /* data:     (BYTE *) Pointer to the first character that should  */
  250. /*           be printed.                                          */
  251. /*                                                                */
  252. /* length    (ULONG) How many characters (bytes) you want to send */
  253. /*           to the printer.                                      */
  254.  
  255. BYTE PrintText(
  256.   union printerIO *ioreq, /* Pointer to the printer request block.          */
  257.   BYTE *data,             /* Pointer to the data which should be printed.   */
  258.   ULONG length            /* How many characters (bytes) should be printed. */
  259. )
  260. {
  261.   /* We want to print some text: (send data to PRT:) */
  262.   ioreq->ios.io_Command = CMD_WRITE;
  263.  
  264.   /* Give the start address of our data: */
  265.   ioreq->ios.io_Data = (APTR) data;
  266.  
  267.   /* Set number of chracters that should be printed: */
  268.   ioreq->ios.io_Length = length;
  269.  
  270.   /* Do our request, and return 0 if everything is OK, else */
  271.   /* return an error number: (This is a task sleep.)        */
  272.   return( (BYTE) DoIO( ioreq ) );
  273. }
  274.  
  275.  
  276.  
  277. /* PrintRaw() sends untranslated characters to the printer. Note   */
  278. /* that this is usually not a very good idea. Since the characters */
  279. /* will not be translated by Preferences, you can not be sure that */
  280. /* the characters you send will be the same when printed.          */ 
  281. /*                                                                 */
  282. /* Synopsis: error = PrintRaw( io, data, length );                 */
  283. /*                                                                 */
  284. /* error:    (BYTE) PrintRaw() returns 0 if everything was OK,     */
  285. /*           else an error number is returned.                     */
  286. /*                                                                 */
  287. /* io:       (union printerIO *) Pointer to a printer request      */
  288. /*           block.                                                */
  289. /*                                                                 */
  290. /* data:     (BYTE *) Pointer to the first character that should   */
  291. /*           be printed.                                           */
  292. /*                                                                 */
  293. /* length    (ULONG) How many characters (bytes) you want to send  */
  294. /*           to the printer.                                       */
  295.  
  296. BYTE PrintRaw(
  297.   union printerIO *ioreq, /* Pointer to the printer request block.          */
  298.   BYTE *data,             /* Pointer to the data which should be printed.   */
  299.   ULONG length            /* How many characters (bytes) should be printed. */
  300. )
  301. {
  302.   /* We want to print some raw (untranslated) text: */
  303.   ioreq->ios.io_Command = PRD_RAWWRITE;
  304.  
  305.   /* Give the start address of our data: */
  306.   ioreq->ios.io_Data = (APTR) data;
  307.  
  308.   /* Set number of chracters that should be printed: */
  309.   ioreq->ios.io_Length = length;
  310.  
  311.   /* Do our request, and return 0 if everything is OK, else */
  312.   /* return an error number: (This is a task sleep.)        */
  313.   return( (BYTE) DoIO( ioreq ) );
  314. }
  315.  
  316.